using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using Rss;

namespace MSNSearch.Activities
{
    [Designer(typeof(ConsoleActivityDesigner), typeof(IDesigner))]
    [ToolboxBitmap(typeof(Search), "Resources.Custom Activity Background - Live Expo.jpg")]
    public partial class ConvertSearchResponseToOPML : Activity
	{
		public ConvertSearchResponseToOPML()
		{
			InitializeComponent();
		}

        
        public static DependencyProperty SearchResponseToConvertProperty = System.Workflow.ComponentModel.DependencyProperty.Register("SearchResponseToConvert", typeof(SearchResponse), typeof(ConvertSearchResponseToOPML));
        [Description("This is the search response object to convert to OPML.")]
        [Category("Miscellaneous")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public SearchResponse SearchResponseToConvert
        {
            get
            {
                return ((SearchResponse)(base.GetValue(ConvertSearchResponseToOPML.SearchResponseToConvertProperty)));
            }
            set
            {

                base.SetValue(ConvertSearchResponseToOPML.SearchResponseToConvertProperty, value);

            }
        }

        public static DependencyProperty OPMLNameProperty = System.Workflow.ComponentModel.DependencyProperty.Register("OPMLName", typeof(string), typeof(ConvertSearchResponseToOPML));
        [Description("This is the name to associate with the OPML.")]
        [Category("Miscellaneous")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string OPMLName
        {
            get
            {
                return ((string)(base.GetValue(ConvertSearchResponseToOPML.OPMLNameProperty)));
            }
            set
            {

                base.SetValue(ConvertSearchResponseToOPML.OPMLNameProperty, value);

            }
        }


        public static DependencyProperty ResultProperty = System.Workflow.ComponentModel.DependencyProperty.Register("Result", typeof(OPML), typeof(ConvertSearchResponseToOPML));
        [Description("The response from the search.")]
        [Category("Result")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public OPML Result
        {
            get
            {
                return ((OPML)(base.GetValue(ConvertSearchResponseToOPML.ResultProperty)));
            }
            set
            {
                base.SetValue(ConvertSearchResponseToOPML.ResultProperty, value);
            }
        }

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {

            OPML opmlResponse = new OPML();
            opmlResponse.Head.Title = OPMLName;
            opmlResponse.Head.DateCreated = System.DateTime.Now.ToString();

            foreach (SourceResponse sr in SearchResponseToConvert.Responses)
            {
                foreach (Result result in sr.Results)
                {
                    OPMLOutline ol = new OPMLOutline();
                    ol.Description = result.Description;
                    ol.Text = result.Title;
                    ol.XmlUrl = result.Url;
                    if (result.DateTime != null)
                    { ol.CreatedDate = result.DateTime.ToString(); }
                    opmlResponse.Body.Outlines.Add(ol);
                }

            }
            Result = opmlResponse;
            //
            // NOTE: Use static method to trigger system to start up
            //

            return base.Execute(executionContext);

        }
    }
}
